#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Debug.h"
#include "xmlsock_s.h"
#include "xmlsock.h"
#include "mxml.h"

#if (1 == XML_SOCKET_VERSION)

static int g_symbol = -1;


#include "xml_parser.h"
#include "usocket.h"
#include "umessage.h"
#include "uclient.h"
#include "devdef.h"
int xmlsoc_init(int nDevType)
{
    xml_set_docptr(5,  CONF_NETWORKSET, CONF_SIPSETTING, CONF_Record, CONF_Contact, CONF_Phonesetting);
    if(g_symbol < 0)
    {
        g_symbol = ucli_create("LVswitch", nDevType, 0);
    }
	return 0;
}

int xmlsoc_set_sends(const char *xmlfile, const char *xpathExpr, const char *szValue)
{
     char *_pbuf = NULL;
     int _ret = umsg_pack_json(&_pbuf,
                UMSG_SET_JSON_NORMAL(UMSG_SET_JSON_STRING("file", xmlfile)),
                UMSG_SET_JSON_NORMAL(UMSG_SET_JSON_STRING("query", xpathExpr)),
                UMSG_SET_JSON_NODE(
                    UMSG_SET_JSON_NODE_VALUE(UMSG_SET_JSON_STRING("value_str", szValue)),
                    UMSG_SET_JSON_NODE_NULL()),
                UMSG_SET_JSON_NULL());
    umsg_pack_msg(_pbuf, _ret, 2, MSG_DATA_MOD);
    ucli_send(g_symbol, _pbuf, _ret + sizeof(Start_st) + sizeof(unsigned char));
    free(_pbuf);
    return  0;
}


int  xmlsoc_gets(const char *xmlfile, const char *nodepath, char *szValue, int nLen, const char *szDefaultValue)
{
    xmlDocPtr doc = NULL ;
    xmlXPathObjectPtr xpathObj = NULL;
	xmlNodeSetPtr nodeset = NULL;
    doc = xml_get_docptr((char *)xmlfile);
    if(NULL == doc)
    {
        Safe_Printf("no doc=%s", xmlfile);
        return -1;
    }
    xpathObj = xml_get_query_result(doc, BAD_CAST  nodepath);
    if(NULL == xpathObj)
    {
        Safe_Printf("xml=%s, node:%s, error.xpathobj", xmlfile, nodepath);
        return -1;
    }
    nodeset = xpathObj->nodesetval;
    if(nodeset->nodeNr <= 0)
    {
        Safe_Printf("xml=%s, node:%s, nr=%d  ", xmlfile, nodepath, nodeset->nodeNr);
    }
    for (int i = 0; i < nodeset->nodeNr; i++)
    {
        xmlChar *uri = xmlNodeGetContent(nodeset->nodeTab[i]);
        snprintf(szValue, nLen, "%s", uri);
        Safe_Printf("xml=%s, node:%s, nodeNr=%d,value=[%s]", xmlfile, nodepath, nodeset->nodeNr, szValue);
        xmlFree(uri);
        break ;
    }
    xml_release_query(xpathObj);
    return 0;
}

int xmlsoc_sets(const char *xmlfile, const char *nodepath, char *szValue)
{
    xmlDocPtr doc = NULL ;
    xmlXPathObjectPtr xpathObj = NULL;
	xmlNodeSetPtr nodeset = NULL;
    doc = xml_get_docptr((char *)xmlfile);
    if(NULL == doc)
    {
        Safe_Printf("no doc=%s", xmlfile);
        return -1;
    }
    xpathObj = xml_get_query_result(doc, BAD_CAST nodepath);
    if (NULL == xpathObj)
    {
        Safe_Printf("xml=%s, node:%s, error.xpathobj", xmlfile, nodepath);
        return -1;
    }
    nodeset = xpathObj->nodesetval;
    if(nodeset->nodeNr <=0 )
    {
        Safe_Printf("error,xml=%s, node:%s,Nr=%d", xmlfile, nodepath, nodeset->nodeNr);
    }
    for (int i = 0; i < nodeset->nodeNr; i++)
    {
        xml_set_node(nodeset->nodeTab[i],
                    XML_SET_NODE(
                        XML_SET_NODE_VALUE(szValue),
                        XML_SET_NODE_NULL()), 
                    XML_SET_NULL());
    }
	xml_release_query(xpathObj);
    xmlsoc_set_sends(xmlfile, nodepath, szValue);
    return 0;
}

int  xmlsoc_replace(const char *szXmlfile_Old, const char *xmlfile_tmp, const char *xmlfile_config)
{
    // send xml stop save
    char *pbuf = NULL;
    int dret = umsg_pack_json(&pbuf,
                        UMSG_SET_JSON_NORMAL(UMSG_SET_JSON_STRING("file", xmlfile_tmp)),
                        UMSG_SET_JSON_NULL());   
    umsg_pack_msg(pbuf, dret, 2, MSG_DATA_STOPSAVING);
    ucli_send(g_symbol, pbuf, dret + sizeof(Start_st) + sizeof(unsigned char));
    if (pbuf)
        free(pbuf);
	pbuf = NULL;
    // replace xml file
    int rret = -1;
    rret = rename(szXmlfile_Old, xmlfile_tmp);
    if(rret < 0)
    {
        Safe_Printf("error . rename %s -> %s", szXmlfile_Old, xmlfile_tmp);
    }
    rret = rename(szXmlfile_Old, xmlfile_config);
    if(rret < 0)
    {
        Safe_Printf("error . rename %s -> %s", szXmlfile_Old, xmlfile_config);
    }
    xml_reset_docptr((char *)xmlfile_tmp);

    // send xml reload
    dret = umsg_pack_json(&pbuf,
                        UMSG_SET_JSON_NORMAL(UMSG_SET_JSON_STRING("file", xmlfile_tmp)),
                        UMSG_SET_JSON_NULL());
    umsg_pack_msg(pbuf, dret, 2, MSG_DATA_RELOAD);
    ucli_send(g_symbol, pbuf, dret + sizeof(Start_st) + sizeof(unsigned char));
    if (pbuf)
        free(pbuf);
	pbuf = NULL;
    return rret;
}



#endif